<?php

namespace Condistelec\MainBundle\Controller;

use Condistelec\MainBundle\Entity\Perfil;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\SecurityExtraBundle\Annotation\Secure;

class SecurityController extends Controller {
	
    /**
     * @Route("/login", name="_login")
     * @Template()
     */
    public function loginAction() {
    	
    	//die(var_dump($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)));
    	
        if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
        }

        return array(
            'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
        );
    }

    /**
     * @Route("/login_check", name="_security_check")
     */
    public function securityCheckAction() {
        // The security layer will intercept this request
    }

    /**
     * @Route("/logout", name="_logout")
     */
    public function logoutAction() {
        // The security layer will intercept this request
    }

    /**
    *  @Route("/redirect", name="_redirect")
    */
    public function redirectAction() {
      $user = $this->getUser();
      
      if($user->getPerfil()->getIdPerfil() == Perfil::PERFIL_USUARIO) {
        return $this->redirect($this->generateUrl('_cond_app_usr_home'));
      }
      if($user->getPerfil()->getIdPerfil() == Perfil::PERFIL_ADMIN) {
        return $this->redirect($this->generateUrl('_cond_app_user_list'));
      }
      return $this->redirect($this->generateUrl('_logout'));
    }
}